home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Kibitz / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1992-10-22  |  5.9 KB  |  234 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        window.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __ERRORS__
  21. #include <Errors.h>
  22. #endif
  23.  
  24. #ifndef __GWLAYERS__
  25. #include <GWLayers.h>
  26. #endif
  27.  
  28. #ifndef THINK_C
  29. #ifndef __SYSEQU__
  30. #include <SysEqu.h>
  31. #endif
  32. #endif
  33.  
  34. #ifndef __UTILITIES__
  35. #include <Utilities.h>
  36. #endif
  37.  
  38.  
  39.  
  40. /*****************************************************************************/
  41.  
  42.  
  43.  
  44. extern short    gPrintPage;        /* Non-zero means we are printing. */
  45. extern LayerObj    gBoardLayer;
  46.  
  47.  
  48.  
  49. /*****************************************************************************/
  50. /*****************************************************************************/
  51.  
  52.  
  53.  
  54. /* This function creates a new application window.  An application window
  55. ** contains a document which is referenced by a handle in the refCon field. */
  56.  
  57. #pragma segment Window
  58. OSErr    AppNewWindow(FileRecHndl frHndl, WindowPtr *retWindow, WindowPtr behind)
  59. {
  60.     WindowPtr    oldPort, window;
  61.     OSErr        err;
  62.     Rect        rct;
  63.  
  64.     /* We will allocate our own window storage instead of letting the Window
  65.     ** Manager do it because GetNewWindow may load in temp. resources before
  66.     ** making the NewPtr call, and this can lead to heap fragmentation. */
  67.  
  68.     GetPort(&oldPort);
  69.  
  70.     err = memFullErr;        /* Assume that we will fail.  Good attitude. */
  71.  
  72.     SetRect(&rct, 0, 0, 0, 0);
  73.     window = GetStaggeredWindow(rWindow, nil, false, FrontWindow(), behind, true, rct, (long)frHndl);
  74.     if (window) {
  75.         (*frHndl)->fileState.window = window;
  76.         AppNewWindowTitle(window);
  77.         if (!(err = AppNewWindowControls(frHndl, window, behind))) {
  78.             if (gPrintPage) MoveWindow(window, 16384, 16384, true);
  79.                 /* So the window can be hidden while printing, yet
  80.                 ** PrintMonitor can get the document name. */
  81.             if ((*frHndl)->doc.justBoardWindow)
  82.                 ZoomToWindowDevice(window, rJustBoardWindowWidth, rWindowHeight, inZoomOut, true);
  83.             ShowWindow(window);
  84.             if (gPrintPage)
  85.                 MoveWindow(window, 16384, 16384, true);
  86.                     /* Moving invisible windows to the front doesn't always
  87.                     ** get them to the front.  Now that it is visible, moving
  88.                     ** it will definitely get it to the front. */
  89.         }
  90.     }
  91.  
  92.     SetPort(oldPort);
  93.     if (retWindow) *retWindow = window;
  94.  
  95.     return(err);
  96. }
  97.  
  98.  
  99.  
  100. /*****************************************************************************/
  101.  
  102.  
  103.  
  104. /* This function updates the window title to reflect the new document name.
  105. ** The new document name is stored in the fileState portion of the document.
  106. ** This is automatically set to 'Untitled # N' for new documents, and is
  107. ** updated when a user does a save-as. */
  108.  
  109. #pragma segment Window
  110. void    AppNewWindowTitle(WindowPtr window)
  111. {
  112.     FileRecHndl    frHndl;
  113.     Str255        wTitle;
  114.  
  115.     if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  116.         pcpy(wTitle, (*frHndl)->fileState.fss.name);
  117.         SetWTitle(window, wTitle);
  118.     }
  119. }
  120.  
  121.  
  122.  
  123. /*****************************************************************************/
  124.  
  125.  
  126.  
  127. /* This function returns the state of the window's document.  If the document
  128. ** is dirty, then true is returned.  If the document is clean, or the window
  129. ** has no document, then false is returned. */
  130.  
  131. #pragma segment Window
  132. Boolean    AppWindowDirty(WindowPtr window)
  133. {
  134.     FileRecHndl    frHndl;
  135.  
  136.     if (frHndl = (FileRecHndl)GetWRefCon(window))
  137.         return(AppDocumentDirty(frHndl));
  138.  
  139.     return(false);
  140. }
  141.  
  142.  
  143.  
  144. /*****************************************************************************/
  145.  
  146.  
  147.  
  148. /* Close all the windows.  This is called prior to quitting the application.
  149. ** This function returns true if all windows were closed.  The user may decide
  150. ** to abort a save, thus stopping the closing of the windows.  If the user
  151. ** does this, false will be returned, indicating that all windows were not
  152. ** closed after all. */
  153.  
  154. #pragma segment Window
  155. Boolean    CloseAllWindows(void)
  156. {
  157.     WindowPtr    window;
  158.  
  159. #ifdef __SYSEQU__
  160.     while (window = *(WindowPtr *)WindowList) {
  161. #else
  162.     while (window = (WindowPtr)WindowList) {
  163. #endif
  164.         /* While we have a front window, try closing it. */
  165.  
  166.         if (!CloseOneWindow(window, iQuit)) return(false);
  167.             /* When CloseOneWindow returns false, this means that the window
  168.             ** didn't close.  The only cause of this is if the window had a
  169.             ** document that needed saving, and the user cancelled the save.
  170.             ** If the window succeeded in getting closed, then we are
  171.             ** returned true.  Either way, we return the result. */
  172.     }
  173.  
  174.     return(true);
  175. }
  176.  
  177.  
  178.  
  179. /*****************************************************************************/
  180.  
  181.  
  182.  
  183. /* Closes one window.  This window may be an application window, or it may be
  184. ** a system window.  If it is an application window, it may have a document
  185. ** that needs saving. */
  186.  
  187. #pragma segment Window
  188. Boolean    CloseOneWindow(WindowPtr window, short saveMode)
  189. {
  190.     FileRecHndl    frHndl;
  191.     OSErr        err;
  192.  
  193.     if (IsAppWindow(window)) {
  194.         /* First, if the window is an application window, try saving
  195.         ** the document.  Remember that the user may cancel the save. */
  196.  
  197.         if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  198.             err = AppSaveDocument(frHndl, window, saveMode);
  199.             if (err) {
  200.                 if (err != userCanceledErr)
  201.                     Alert(rErrorAlert, (ModalFilterProcPtr)AlertFilter);
  202.                 return(false);
  203.             }        /* Stop closing windows on error or user cancel. */
  204.  
  205.             SetOpponentType(frHndl, kOnePlayer);
  206.             AppDisposeDocument(frHndl);
  207.                 /* The document is saved, or the user doesn't care about
  208.                 ** that document, so dispose of the document. */
  209.         }
  210.     }
  211.     DisposeAnyWindow(window);
  212.  
  213.     return(true);
  214. }
  215.  
  216.  
  217.  
  218. /*****************************************************************************/
  219.  
  220.  
  221.  
  222. #pragma segment Window
  223. WindowPtr    SetFilePort(FileRecHndl frHndl)
  224. {
  225.     WindowPtr    oldPort;
  226.  
  227.     GetPort(&oldPort);
  228.     SetPort((*frHndl)->fileState.window);
  229.     return(oldPort);
  230. }
  231.  
  232.  
  233.  
  234.